import Image
import ImageFilter

import matplotlib

execfile('imTools.py');
# os.chdir(r'C:\My Code Projects\python-opencv\trunk\lib\python\digitalMatting')


box1=(208,73,485,284)  #the gray patch on the bird's body (left, upper, right, lower)

imR=Image.open(r'C:\My ClipArt\digitalMatting\samples\red.jpg').crop(box1);
imG=Image.open(r'C:\My ClipArt\digitalMatting\samples\green.jpg').crop(box1);
imB=Image.open(r'C:\My ClipArt\digitalMatting\samples\blue.jpg').crop(box1);


#if we want to smooth the filter
#imR = imR.filter(ImageFilter.SMOOTH_MORE)
#imG = imG.filter(ImageFilter.SMOOTH_MORE)

#lets difference all of the color combinations
absDiffRG = np.abs(numpy.asarray(imR) - numpy.asarray(imG))
absDiffRG=absDiffRG.astype(float);
absDiffRG=np.power(absDiffRG,2).sum(axis=2);

absDiffRB = np.abs(numpy.asarray(imR) - numpy.asarray(imB))
absDiffRB=absDiffRB.astype(float);
absDiffRB=np.power(absDiffRB,2).sum(axis=2);

absDiffGB = np.abs(numpy.asarray(imG) - numpy.asarray(imB))
absDiffGB=absDiffGB.astype(float);
absDiffGB=np.power(absDiffGB,2).sum(axis=2);

diffIm=np.sqrt(absDiffRG+absDiffRB+absDiffGB);



#********Plotting the information**********

#first we will compare a patch of the background verus a patch on the forground object
#we are interested in:
#1. How the RGB histograms compare for the two patches.
#2. How the difference compares between the 2 patches

patchBackground=(167,12,257,74) #(left, upper, right, lower)
patchBird=(145,123,173,155)

#pictures for show
imR.crop(patchBackground).save(r'C:\My ClipArt\digitalMatting\samples\backgroundPatchR.jpg')
imR.crop(patchBird).save(r'C:\My ClipArt\digitalMatting\samples\foregroundPatchR.jpg')

imG.crop(patchBackground).save(r'C:\My ClipArt\digitalMatting\samples\backgroundPatchG.jpg')
imG.crop(patchBird).save(r'C:\My ClipArt\digitalMatting\samples\foregroundPatchG.jpg')


matplotlib.pylab.figure();
matplotlib.pylab.hist(np.sqrt(absDiffRG[patchBackground[1]:patchBackground[3],patchBackground[0]:patchBackground[2]].flatten()), 256);
matplotlib.pyplot.title('Histogram of background patch abs diff',fontsize='xx-large')
matplotlib.pyplot.xlabel('pixel value',fontsize='xx-large')
matplotlib.pyplot.ylabel('# of pixels',fontsize='xx-large')


matplotlib.pylab.figure();
matplotlib.pylab.hist(np.sqrt(absDiffRG[patchBird[1]:patchBird[3],patchBird[0]:patchBird[2]].flatten()), 256);
matplotlib.pyplot.title('Histogram of bird patch',fontsize='xx-large')
matplotlib.pyplot.xlabel('pixel value',fontsize='xx-large')
matplotlib.pyplot.ylabel('# of pixels',fontsize='xx-large')

compareImageHist(imR, imG, cropBox=patchBackground,legendText=('red background', 'green background'))


#look at a histogram of the bird
compareImageHist(imR, imG, cropBox=patchBird,legendText=('bird infront of red', 'bird in front of green'))

#imR.save(r'C:\My ClipArt\digitalMatting\samples\clipRed.jpg')
#imG.save(r'C:\My ClipArt\digitalMatting\samples\clipGreen.jpg')
#imG.save(r'C:\My ClipArt\digitalMatting\samples\clipGreen.jpg')


matplotlib.pyplot.figure()
matplotlib.pyplot.imshow(np.sqrt(absDiffRG),cmap=cm.gray)
matplotlib.pyplot.colorbar()

matplotlib.pyplot.figure()
matplotlib.pyplot.imshow(diffIm,cmap=cm.gray)
matplotlib.pyplot.colorbar()

#look at the spectrum
matplotlib.pylab.figure();
from scipy import fftpack

np.sqrt(absDiffRG[patchBackground[1]:patchBackground[3],patchBackground[0]:patchBackground[2]].flatten())
 y=scipy.fftpack.fft2(absDiffRG, shape=None, axes=(-2, -1), overwrite_x=0)